﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using Csla.Data;
using log4net;

namespace Sireass.DalSql
{
    public class DalManager : Sireass.Dal.IDalManager
    {
		// Create a logger for use in this class
		private static readonly ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
		
        private static string _typeMask = typeof(DalManager).FullName.Replace("DalManager", @"{0}");

        public T GetProvider<T>() where T : class
        {
            var typeName = string.Format(_typeMask, typeof(T).Name.Substring(1));
            var type = Type.GetType(typeName);
			if (log.IsDebugEnabled)
			{
				log.Debug("typeName: " + typeName);
				log.Debug("type: " + type);
			}
            if (type != null)
                return Activator.CreateInstance(type) as T;
            else
                throw new NotImplementedException(typeName);
        }

        public ConnectionManager<SqlConnection> ConnectionManager { get; private set; }

        public DalManager()
        {
            ConnectionManager = ConnectionManager<SqlConnection>.GetManager("SqlServer");
        }

        public void Dispose()
        {
            ConnectionManager.Dispose();
            ConnectionManager = null;
        }
    }
}
